// pages/addressEdit/addressEdit.js
const chooseLocation = requirePlugin('chooseLocation');
const app = getApp()
import {
        save,
        update,
        del
} from '../../../api/address'
const util = require('../../../utils/util.js')

Page({

        /**
         * 页面的初始数据
         */
        data: {
                array: ['有电梯', '1楼', '2楼', '3楼', '4楼', '5楼', '6楼', '7楼', '8楼', '9楼'],
                title: "",
                type: "",
                isDefault: true, //是否是默认地址
                id: "",
                phone: "",
                level: 0,
                latitude: "",
                longitude: "",
                consignee: "",
                building: "",
                areaName: "",
                address: "",
                addressDetail: "",
                roomNumber: "",
        },

        /**
         * 生命周期函数--监听页面加载
         */
        onLoad: function () {
                let _this = this
                const eventChannel = this.getOpenerEventChannel()
                eventChannel.on('sendAddressDetail', function (data) {
                        if (data.type == "add") {
                                _this.setData({
                                        type: data.type,
                                        title: "添加收货地址"
                                })
                        } else if (data.type == "edit") {
                                _this.setData({
                                        title: "编辑收货地址",
                                        type: data.type,
                                        id: data.data.id,
                                        isDefault: data.data.isDefault, //是否是默认地址
                                        phone: data.data.phone,
                                        latitude: data.data.lat,
                                        longitude: data.data.lng,
                                        level: data.data.level,
                                        consignee: data.data.consignee,
                                        building: data.data.building,
                                        areaName: data.data.areaName,
                                        address: data.data.address,
                                        roomNumber: data.data.roomNumber,
                                        memberId: data.data.memberId,
                                        addressDetail: data.data.addressDetail,
                                })
                        }
                })
        },

        /**
         * 生命周期函数--监听页面初次渲染完成
         */
        onReady: function () {

        },

        /**
         * 生命周期函数--监听页面显示
         */
        onShow: function () {
                let _this = this
                const location = chooseLocation.getLocation();
                console.info("地图选点回来了" + JSON.stringify(location))
                if (location != null) {
                        let areaName = ""
                        if(location.province == location.city){
                                areaName = location.province + location.district
                        }else{
                                areaName = location.province + location.city + location.district
                        }
                        let address = ""
                        let building = location.address + "(" + location.name + ")"
                        console.info("地图选点回来了" + address.indexOf(areaName))
                        if(location.address.indexOf(areaName) == -1){
                                building = areaName + building
                        }
                        console.info("地图选点回来了" + building)
                        this.setData({
                                latitude: location.latitude,
                                longitude: location.longitude,
                                building: building,
                                areaName: areaName,
                                address: building,
                                addressDetail: location.address + "(" + location.name + ")"
                        })
                }
        },

        //跳转到区域选择页面
        toSelectArea(e) {
                console.log('我要去选择地址信息了')
                const key = app.globalData.mapKey; //使用在腾讯位置服务申请的key
                const referer = '位置选择'; //调用插件的app的名称
                const location = JSON.stringify({
                        latitude: 39.89631551,
                        longitude: 116.323459711
                });
                const category = '生活服务,娱乐休闲';
                console.log(key)
                console.log(referer)
                wx.navigateTo({
                        url: `plugin://chooseLocation/index?key=${key}&referer=${referer}`
                });
        },
        bindPickerChange: function (e) {
                console.log('picker发送选择改变，携带值为', e.detail.value)
                this.setData({
                        level: e.detail.value
                })
        },
        //默认地址的滑动按钮
        switchChange(e) {
                this.setData({
                        isDefault: e.detail.value
                })
        },

        //表单提交事件
        submit() {
                console.log("添加地址的数据addressDetail--" + JSON.stringify(this.data.addressDetail))
                if (util.isNull(this.data.consignee)) {
                        wx.showToast({
                                title: '收件人姓名不能为空',
                                icon: 'none', //如果要纯文本，不要icon，将值设为'none'
                                duration: 2000
                        })
                } else if (util.isNull(this.data.phone)) {
                        wx.showToast({
                                title: '收件人号码不能为空',
                                icon: 'none', //如果要纯文本，不要icon，将值设为'none'
                                duration: 2000
                        })
                } else if (!(/^((1))\d{10}$/.test(this.data.phone))) {
                        wx.showToast({
                                title: '收件人号码输入有误',
                                icon: 'none', //如果要纯文本，不要icon，将值设为'none'
                                duration: 2000
                        })
                } else if (util.isNull(this.data.building)) {
                        wx.showToast({
                                title: '请点选收货地址',
                                icon: 'none', //如果要纯文本，不要icon，将值设为'none'
                                duration: 2000
                        })
                } else if (util.isNull(this.data.roomNumber)) {
                        wx.showToast({
                                title: '小区、门牌号信息不能为空',
                                icon: 'none', //如果要纯文本，不要icon，将值设为'none'
                                duration: 2000
                        })
                } else {
                        let data = {}
                        data.consignee = this.data.consignee
                        data.phone = this.data.phone
                        data.address = this.data.building + ' ' + this.data.roomNumber
                        data.areaName = this.data.areaName
                        data.building = this.data.building
                        data.level = this.data.level
                        data.isDefault = this.data.isDefault
                        data.roomNumber = this.data.roomNumber
                        data.memberId = this.data.memberId
                        data.lat = this.data.latitude
                        data.lng = this.data.longitude
                        // console.log("添加地址的数据data--" + JSON.stringify(data))
                        console.log("this.data.type--" + JSON.stringify(this.data.type))
                        if (this.data.type == "add") {
                                save(data).then(
                                        res => {
                                                console.log("添加地址的res--" + JSON.stringify(res))
                                                if (res.type == 'success') {
                                                        wx.navigateBack({
                                                                delta: 1,
                                                        })
                                                }
                                        }
                                ).catch(error => {
                                        console.log(error)
                                        wx.showToast({
                                                title: error.content,
                                                icon: 'none', //如果要纯文本，不要icon，将值设为'none'
                                                duration: 2000
                                        })
                                })
                        } else if (this.data.type == "edit") {
                                data.id = this.data.id
                                console.log("添加地址的数据data--" + JSON.stringify(data))
                                update(data).then(
                                        res => {
                                                console.log("修改地址的res--" + JSON.stringify(res))
                                                if (res.type == 'success') {
                                                        wx.navigateBack({
                                                                delta: 1,
                                                        })
                                                }
                                        }
                                ).catch(error => {
                                        console.log(error)
                                        wx.showToast({
                                                title: error.content,
                                                icon: 'none', //如果要纯文本，不要icon，将值设为'none'
                                                duration: 2000
                                        })
                                })
                        }
                }


        },

        //表单重置事件
        formReset(e) {
                console.log('form发生了reset事件，携带数据为：', e.detail.value)
                this.setData({
                        area: ''
                })
        },

        //地址删除的事件
        del() {
                console.info(this.data.id)
                del(this.data.id).then(
                        res => {
                                console.log("删除地址res--" + JSON.stringify(res))
                                if (res.type == 'success') {
                                        wx.navigateBack({
                                                delta: 1,
                                        })
                                }
                        }
                ).catch(error => {
                        console.log(error)
                        wx.showToast({
                                title: error.content,
                                icon: 'none', //如果要纯文本，不要icon，将值设为'none'
                                duration: 2000
                        })
                })
        },
        nameClick(data) {
                console.log(data)
                this.data.consignee = data.detail.value
                this.setData({
                        consignee: this.data.consignee
                })
        },
        phoneClick(data) {
                this.data.phone = data.detail.value
                this.setData({
                        phone: this.data.phone
                })
        },
        roomClick(data) {
                console.log(data)
                this.data.roomNumber = data.detail.value
                this.setData({
                        roomNumber: this.data.roomNumber
                })
        }
})
